package com.power.ding.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.ding.api.IDi_userFeign;
import com.power.ding.model.Di_user;
import com.power.ding.model.Gzt_User;
import com.power.ding.model.pageInfo.Di_userPageInfo;
import com.power.ding.service.Di_userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   用户信息控制器
 */

//@PowerLog
@Slf4j
@RestController
@Api(value = "Di_userController",tags={"用户信息"})
public class Di_userController implements IDi_userFeign {

    @Resource
    private Di_userService di_userService;


    @Override
    @ApiOperation("查询用户信息分页列表")
    @SaCheckPermission("di_user_list")
    public ResponseDTO list(Di_userPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Di_user> page = this.di_userService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Di_user di_user : page) {
                di_user.geneText();
                list.add(di_user);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看用户信息")
    @SaCheckPermission("di_user_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Di_user di_user  = di_userService.getById(id);
        if (di_user != null) {
            di_user.geneText();
        }
        return ResponseDTO.succData(di_user);
    }

    @Override
    @ApiOperation("删除用户信息")
    @SaCheckPermission("di_user_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        di_userService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改用户信息")
    @SaCheckPermission("di_user_save")
    public ResponseDTO save(Di_user di_user) {
        Assert.notNull(di_user, "di_user不能为空");
        di_userService.saveOrUpdate(di_user);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除用户信息")
    @SaCheckPermission("di_user_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        di_userService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("di_user_list")
    public void output(HttpServletResponse response, Di_userPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "用户信息";
        String[] heads = {"序号","外部编号","删除标识","最后修改时间","用户姓名","登录名","密码","工号","昵称","联系电话","手机","电子邮件","家庭住址","家庭电话","家庭邮编","家庭邮编","拼音全拼","拼音首字母","职级编码","职级","职务","性别编码","性别","教育程度编码","教育程度","状态","状态","出生日期","年龄","参加工作时间","进入本单位时间","显示顺序","密级等级","密级等级","直接领导","直接领导id","备注","头像","身份证号","聊天工具id","办公地点","ca证书序列号","签名图片","签名图片","是否在编用户","是否在编用户","职称编码","职称","人员编制编号","人员编制名称","机构id"};
        String[] fields = {"id","foreignid","rowstate","lastupdatetime","username","logname","password","cardid","nickname","tel","mobile","email","homeadd","hometel","homezipcode","fax","pinyin","pinyininitial","careerrank","careerranktext","unitrank","sex","sextext","education","educationtext","status","statustext","birthday","age","joinwork","joincompany","showorder","mjlevel","mjleveltext","leadertext","leader","remark","photo","idnumber","imid","workplace","certsn","signphoto","cabinetcode","isformation","isformationtext","professionalrank","professionalranktext","formation","formationtext","unitid"};
        Page<Di_user> page = this.di_userService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "用户信息";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","外部编号","删除标识","最后修改时间","用户姓名","登录名","密码","工号","昵称","联系电话","手机","电子邮件","家庭住址","家庭电话","家庭邮编","家庭邮编","拼音全拼","拼音首字母","职级编码","职级","职务","性别编码","性别","教育程度编码","教育程度","状态","状态","出生日期","年龄","参加工作时间","进入本单位时间","显示顺序","密级等级","密级等级","直接领导","直接领导id","备注","头像","身份证号","聊天工具id","办公地点","ca证书序列号","签名图片","签名图片","是否在编用户","是否在编用户","职称编码","职称","人员编制编号","人员编制名称","机构id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","foreignid","rowstate","lastupdatetime","username","logname","password","cardid","nickname","tel","mobile","email","homeadd","hometel","homezipcode","fax","pinyin","pinyininitial","careerrank","careerranktext","unitrank","sex","sextext","education","educationtext","status","statustext","birthday","age","joinwork","joincompany","showorder","mjlevel","mjleveltext","leadertext","leader","remark","photo","idnumber","imid","workplace","certsn","signphoto","cabinetcode","isformation","isformationtext","professionalrank","professionalranktext","formation","formationtext","unitid"};
        List<Di_user> list = (List<Di_user>)ExcelUtil.Import(fields, file, Di_user.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.di_userService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO findUserByOrgId(String orgId) {
        Assert.notNull(orgId,"orgId不能为空");
        return ResponseDTO.succData(this.di_userService.findUserByOrgId(orgId));
    }

    @Override
    public ResponseDTO getUserAndDeptById(String userid, Gzt_User gzt_user) {
        try {
            return ResponseDTO.succData(this.di_userService.getUserAndDeptById(userid, gzt_user));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询登录人和部门信息失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }

    @Override
    public ResponseDTO getUserByPhones(List<String> phones) {
        try {
            return ResponseDTO.succData(this.di_userService.getUserByPhones(phones));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("通过电话号码查询赣政通用户失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }

    @Override
    public ResponseDTO getSysUserByPhones(String mobile) {
        try {
            return ResponseDTO.succData(this.di_userService.getSysUserByPhones(mobile));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("通过电话号码查询系统用户失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }

    @Override
    public ResponseDTO getRelUserByMobile(String mobile) {
        try {
            return ResponseDTO.succData(this.di_userService.getRelUserByMobile(mobile));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("按照电话号码查询赣政通可登录用户 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }

    @Override
    public ResponseDTO getUserByAdid(String adid) {
        try {
            return ResponseDTO.succData(this.di_userService.getUserByAdid(adid));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/di_user/getUserByAdid 根据区划查询关联用户失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "根据区划查询关联用户失败");
        }

    }

    @Override
    public ResponseDTO getUserByDeptid(String deptid) {
        try {
            return ResponseDTO.succData(this.di_userService.getUserByDeptid(deptid));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/di_user/getUserByAdid 根据区划查询关联用户失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "根据区划查询关联用户失败");
        }
    }
}
